package com.ozy.kxreader.mvp.ui.activity

import android.os.Bundle
import android.support.v7.widget.StaggeredGridLayoutManager
import com.chad.library.adapter.base.entity.SectionEntity
import com.ozy.kxreader.R
import com.ozy.kxreader.base.BaseActivity
import com.ozy.kxreader.di.component.AppComponent
import com.ozy.kxreader.di.component.DaggerCategoryComponent
import com.ozy.kxreader.di.module.CategoryModule
import com.ozy.kxreader.mvp.contract.CategoryContract
import com.ozy.kxreader.mvp.model.entity.Category
import com.ozy.kxreader.mvp.presenter.CategoryPresenter
import com.ozy.kxreader.mvp.ui.adapter.CategoryAdapter
import com.ozy.kxreader.mvp.ui.widget.SpaceDecoration
import com.ozy.kxreader.toast
import kotlinx.android.synthetic.main.activity_category.*
import java.util.ArrayList


class CategoryActivity : BaseActivity<CategoryPresenter>(), CategoryContract.View {

    private lateinit var categoryAdapter: CategoryAdapter

    override fun setupActivityComponent(appComponent: AppComponent?) =
            DaggerCategoryComponent
                    .builder()
                    .appComponent(appComponent)
                    .categoryModule(CategoryModule(this))
                    .build()
                    .inject(this)

    override fun layoutId(): Int = R.layout.activity_category

    override fun initData(savedInstanceState: Bundle?) {
        categoryAdapter = CategoryAdapter()
        var view = layoutInflater.inflate(R.layout.layout_empty_footerview_30dp, null, false)
        categoryAdapter.addFooterView(view)

        recyclerView!!.layoutManager = StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL)
        recyclerView!!.adapter = categoryAdapter
        var spaceDecoration = SpaceDecoration(2, resources.getColor(R.color.line), false, false, false)
        recyclerView!!.addItemDecoration(spaceDecoration)

        categoryAdapter?.setOnItemClickListener({ adapter, view, position ->
            val item = adapter.getItem(position) as SectionEntity<*>
            if (!item.isHeader) {
                val name = (item.t as Category).name
                val gender = categoryAdapter.getHeaderItem(position)?.t as String
                toast("name:" + name + "  , t :" + categoryAdapter.getHeaderItem(position)?.t)
                BookListActivity.start(this, name, gender)
            }
        })
        mPresenter.getCategoryList()
    }

    override fun showCategoryList(data: ArrayList<SectionEntity<Category>>) {
        categoryAdapter.setNewData(data)
    }

}

